package me.jessyan.mvparms.demo.mvp.ui.view;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowManager;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.PopupWindow;

import com.platform.ai.control.R;


/**
 * The author: Zhao X.H.
 * Creation time: 2018/2/14 ${time}
 */

public class InputPopWindow<T extends String> extends PopupWindow {
    private Context mContext;
    private String wifiName;
    public InputPopWindow(Context context,String wifiName) {
        super(context);
        this.mContext = context;
        this.wifiName = wifiName;
        init();
    }

    private void init() {
        View view = LayoutInflater.from(mContext).inflate(R.layout.pop_wifi, null);
        setContentView(view);
        EditText mPwd = (EditText) view.findViewById(R.id.et_wifi_pwd);
        EditText mWIFI = (EditText) view.findViewById(R.id.et_wifi_name);
        mWIFI.setText(wifiName);
        view.findViewById(R.id.tv_cancel).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
            }
        });
        view.findViewById(R.id.tv_confirm).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

            }
        });
        setWidth(LinearLayout.LayoutParams.MATCH_PARENT);
        setHeight(LinearLayout.LayoutParams.WRAP_CONTENT);
        setFocusable(true);//设置为true的时候才能点击列表中的item
        ColorDrawable cd = new ColorDrawable(Color.TRANSPARENT);
        setAnimationStyle(R.style.Popup_Spinner);
//        ColorDrawable dw = new ColorDrawable(-00000);// 半透明
        setBackgroundDrawable(cd);// 设置背景图片，不能在布局中设置，要通过代码来设置
        setOutsideTouchable(true);// 触摸popupwindow外部，popupwindow消失。这个要求你的popupwindow要有背景图片才可以成功，如上
        setBackgroundAlpha(0.5f);
        this.setOnDismissListener(new OnDismissListener() {
            @Override
            public void onDismiss() {
                setBackgroundAlpha(1.0f);
            }
        });
    }


    public void showPopupWindowByCenter(View v){
        showAtLocation(v.getRootView(), Gravity.CENTER, 0, 0);//在v的下面
    }

    /**
     * 设置添加屏幕的背景透明度
     *
     * @param bgAlpha
     *            屏幕透明度0.0-1.0 1表示完全不透明
     */
    public void setBackgroundAlpha(float bgAlpha) {
        WindowManager.LayoutParams lp = ((Activity) mContext).getWindow()
                .getAttributes();
        lp.alpha = bgAlpha;
        ((Activity) mContext).getWindow().setAttributes(lp);
    }
}
